package com.msmall.community.klg.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.service.ArticleManager;
import com.msmall.community.klg.service.KlgManager;
import com.msmall.community.question.model.Question;
import com.msmall.community.user.model.UserSetting;
import com.msmall.community.user.service.UserManager;

public class KlgManagerImpl extends BaseManagerImpl implements KlgManager{

	private static final Log log = LogFactory.getLog(KlgManagerImpl.class);
	
	private UserManager userManager;
	
	private ArticleManager articleManager;

	/**
	 * 用户是否被授权推荐知识
	 */
	public boolean isAuthorizedCommend(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_COMMEND_KLG);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_COMMEND_KLG_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	/**
	 * 用户是否被授权删除知识/回复
	 */
	public boolean isAuthorizedRemove(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_REMOVE_KLG);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_REMOVE_KLG_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	/**
	 * 用户是否被授权创建知识/回复
	 */
	public boolean isAuthorizedCreate(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_CREATE_KLG);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_CREATE_KLG_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	
	/**
	 * 用户是否被授权更新知识/回复
	 */
	public boolean isAuthorizedUpdate(Long userid){
		try{
			UserSetting setting = userManager.getSetting(userid, ConstantDB.USER_SETTING_KEY_UPDATE_KLG);
			if(setting == null)
				return false;
			if(ConstantDB.USER_SETTING_VALUE_UPDATE_KLG_TOP.equals(setting.getPropertyValue())){
				return true;
			}
		}catch(Exception e){
			log.debug(e);
		}
		return false;
	}
	/**
	 * 根据知识分类查询出点击数最高的amount个问题。pagination不统计总数
	 * 由JOB每1小时执行一次
	 */
	public void refreshKlgByClickAmount(int amount){
		try{
			Map<Long, List<Article>> maps = new HashMap<Long, List<Article>>();
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> top = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_NAV);
			List<CascadeCollection<Segment>> children = top.getChildren();
			for(CascadeCollection<Segment> child : children){
				List<CascadeCollection<Segment>> bottoms = child.getChildren();
				for(CascadeCollection<Segment> bottom : bottoms){
					Long segid = bottom.getCurrent().getSegmentid();
					PaginationSupport page = new PaginationSupport();
					page.setAutoCount(false);
					page.setPageIndex(0);
					page.setPageSize(amount);
					getPaginationByClickAmount(page, segid, false);
					maps.put(segid, page.getItems());
				}
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_KLG_SUBSEGMENT_HOT, maps);
		}catch(Exception e){
			log.debug(e);
		}
	}
	/**
	 * 获得segment下点击数最多的知识 enabled
	 * containsChildren 是否统计segid
	 */
	public void getPaginationByClickAmount(PaginationSupport page, Long segid, boolean containsChildren) throws Exception{
		try{
			page.setClazz(Question.class);
			
			String queryString = " select q from Article q, SegmentArticle sa where q.articleid=sa.articleid and q.enabled="+ConstantDB.AVALIABLE;
			StringBuffer buffer = new StringBuffer();
			if(containsChildren){
				buffer.append(" and sa.segmentid in (").append(segid);
				CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
				CascadeCollection<Segment> current = cs.getAllDatas().get(segid);
				List<CascadeCollection<Segment>> children = current.getChildren();
				for(CascadeCollection<Segment> child : children){
					buffer.append(",").append(child.getCurrent().getSegmentid());
				}
				buffer.append(")");
			}else{
				buffer.append(" and sa.segmentid=").append(segid);
			}
			queryString += buffer.toString();
			queryString += " order by q.clickAmount desc";
			page.setQueryString(queryString);
			articleManager.getPagination(page);
		}catch(Exception e){
			log.debug(e);
			throw e;
		}
	}
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}
}
